<template>
  <el-dialog width="800px" :title="dialogTitle" :visible.sync="dialogVisible" @close="handleClose">
    <el-form
      ref="submitForm"
      v-loading="formLoading"
      :model="submitForm"
      :rules="submitFormRules"
      label-width="80px"
      label-position="right"
      class="submit-form"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="来源类型" prop="sourceType">
            <dict-select v-model="submitForm.sourceType" disabled dict-key="produce_source_type" placeholder="请选择来源" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="submitForm.sourceType==='1'" label="模具名称" prop="moldId">
            <mold-query ref="moldQuery" v-model="submitForm.moldId" @choose="handleChooseMold" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数量" prop="quantity">
            <el-input v-model="submitForm.quantity" :disabled="submitForm.sourceType==='0'" placeholder="请输入数量" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="颜色" prop="color">
            <el-input v-model="submitForm.color" :disabled="submitForm.sourceType==='0'" placeholder="请输入颜色" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="材质" prop="material">
            <el-input v-model="submitForm.material" :disabled="submitForm.sourceType==='0'" placeholder="请输入材质" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="尺码" prop="size">
            <el-input v-model="submitForm.size" :disabled="submitForm.sourceType==='0'" placeholder="请输入尺码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="长度" prop="length">
            <el-input v-model="submitForm.length" :disabled="submitForm.sourceType==='0'" placeholder="请输入长度" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="优先级" prop="priority">
            <dict-select v-model="submitForm.priority" dict-key="priority" placeholder="请选择优先级" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="submitForm.remark"
              placeholder="备注"
              maxlength="100"
              show-word-limit
              type="textarea"
              :autosize="{ minRows: 2}"
            />
          </el-form-item>
        </el-col>
      </el-row>
<!--      <el-form-item label="来源类型" prop="sourceType">
        <dict-select v-model="submitForm.sourceType" disabled dict-key="produce_source_type" placeholder="请选择来源" />
      </el-form-item>
      <el-form-item v-if="submitForm.sourceType==='1'" label="模具名称" prop="moldId">
        <mold-query ref="moldQuery" v-model="submitForm.moldId" @choose="handleChooseMold" />
      </el-form-item>
      <el-form-item label="数量" prop="quantity">
        <el-input v-model="submitForm.quantity" :disabled="submitForm.sourceType==='0'" placeholder="请输入数量" />
      </el-form-item>
      <el-form-item label="颜色" prop="color">
        <el-input v-model="submitForm.color" :disabled="submitForm.sourceType==='0'" placeholder="请输入颜色" />
      </el-form-item>
      <el-form-item label="材质" prop="material">
        <el-input v-model="submitForm.material" :disabled="submitForm.sourceType==='0'" placeholder="请输入材质" />
      </el-form-item>
      <el-form-item label="尺码" prop="size">
        <el-input v-model="submitForm.size" :disabled="submitForm.sourceType==='0'" placeholder="请输入尺码" />
      </el-form-item>
      <el-form-item label="长度" prop="length">
        <el-input v-model="submitForm.length" :disabled="submitForm.sourceType==='0'" placeholder="请输入长度" />
      </el-form-item>
      <el-form-item label="优先级" prop="priority">
        <dict-select v-model="submitForm.priority" dict-key="priority" placeholder="请选择优先级" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="submitForm.remark"
          placeholder="备注"
          maxlength="100"
          show-word-limit
          type="textarea"
          :autosize="{ minRows: 2}"
        />
      </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="formLoading" type="text" @click="handleClose">取消</el-button>
      <el-button :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
    </span>
    <div v-if="productAttrVisible">
      <product-attribute-module ref="productAttrModule" :visible="productAttrVisible" @close="handleProductAttrChange" />
    </div>
  </el-dialog>
</template>

<script>
import { updateProduceById, saveProduce, selectProduceById } from '@/api/erp/produce'
import ProductAttributeModule from '@/modules/productAttributeModule.vue'
/**
 * @author yizuomin
 * @date 2023/5/7 19:05
 **/
export default {
  name: 'SubmitModule',
  components: { ProductAttributeModule },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      dialogTitle: '',
      formLoading: false,
      productAttrVisible: false,
      submitForm: {
        id: '',
        sourceType: '1',
        priority: '',
        moldId: '',
        productName: '',
        quantity: '',
        productAttribute: []
      },
      submitFormRules: {
        sourceType: [{ required: true, message: '请选择来源类型', trigger: 'change' }],
        moldId: [{ required: true, message: '请选择产品', trigger: 'change' }],
        quantity: [{ required: true, message: '请输入数量', trigger: 'change' }],
        priority: [{ required: true, message: '请选择优先级', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(id) {
      if (!id) {
        this.dialogTitle = '新增生产'
        return
      }
      this.dialogTitle = '修改生产'
      this.formLoading = true
      selectProduceById(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
          this.submitForm.moldId = resp.data.productInfo.moldId
          this.submitForm.moldName = resp.data.productInfo.moldName
          this.submitForm.color = resp.data.productInfo.color
          this.submitForm.material = resp.data.productInfo.material
          this.submitForm.size = resp.data.productInfo.size
          this.submitForm.length = resp.data.productInfo.length
          if (this.submitForm.sourceType === '1') {
            this.$nextTick(() => {
              this.$refs.moldQuery.init(this.submitForm.moldName)
            })
          }
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    handleSubmit() {
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          this.formLoading = true
          let request = saveProduce
          if (this.submitForm.id) {
            request = updateProduceById
          }
          request(this.submitForm).then(resp => {
            if (resp.success) {
              this.$message.success('操作成功')
              this.$refs.submitForm.resetFields()
              this.handleClose()
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    handleClose() {
      this.$emit('close')
    },
    handleChooseMold(row) {
      if (row) {
        this.submitForm.moldId = row.id
        this.productAttrVisible = true
        this.$nextTick(() => {
          this.$refs.productAttrModule.init(row)
        })
      } else {
        this.productTableVisible = false
      }
    },
    handleProductAttrChange(row) {
      this.productAttrVisible = false
      if (row) {
        this.submitForm.unit = this.moldData.unit
        this.submitForm.color = row.color
        this.submitForm.material = row.material
        this.submitForm.size = row.size
        this.submitForm.length = row.length
        this.submitForm.quantity = row.quantity
        this.submitForm.unitPrice = row.unitPrice
      }
    },
    onInput() {
      this.$forceUpdate()
    }
  }
}
</script>

<style scoped>

</style>
